package com.copa.l1750;

/**
 * @author copa
 * @createDate 2022-12-28 11:23
 * @function 双指针法（自己做的...可能想太多条件限制了...不好理解）
 */
public class Test1 {

    public int minimumLength(String s) {
        if (s.length() == 1) {
            return 1;
        }
        int count = 0;
        int left = 0;
        int right = s.length() - 1;
        while (left <= right) {
            if (s.charAt(left) == s.charAt(right)) {
                // 判断若左右指向的字符均等移动指针
                char compare = s.charAt(left);
                while (left < s.length() - 1 && s.charAt(left) == compare) {
                    left++;
                }
                while (right > 0 && s.charAt(right) == compare) {
                    right--;
                }
                if (left == right && right == s.length() - 1) {
                    // 两指针都能指向最后一个，则可以全删除
                    return 0;
                }
                if (left > right) {
                    // 右指针在左指针左边，证明都可以删的
                    return 0;
                }
            }
            if (left == right) {
                // 如果两指针都能指向同一个，则那一个肯定不能删除
                return 1;
            }
            if (left < s.length() && right > 0
                    && s.charAt(left) == s.charAt(right)) {
                // 若两指针指向的字符一样，则继续遍历
                continue;
            }
            count = right - left + 1;
            break;
        }
        return count;
    }

    public static void main(String[] args) {
//        String s = "cbcbc";
        String s = "bbbbbbbbbbbbbbbbbbb";
        System.out.println(new Test1().minimumLength(s));
    }
}
